var app = angular.module("app", ["ui.router"]);
app.config(["$stateProvider", "$locationProvider", "$urlRouterProvider", function($stateProvider, $locationProvider, $urlRouterProvider) {
	//		$locationProvider.html5Mode(true);
	$urlRouterProvider.when("", "/home/");
	$stateProvider
		.state("main", {
			url: "",
			templateUrl: "html/main.html",
		})
		.state("main.home", { //home页面跳转
			url: "/home/",
			templateUrl: "html/home.html",
			cache:false,
			controller: "home",		
		})
		.state("main.mine", { //个人中心页面跳转
			url: "/mine",
			templateUrl: "html/mine.html",
			controller: "mine"
		})
		.state("main.myOrder", { //个人中心页面跳转到我的订单
			url: "/myOrder",
			templateUrl: "html/mine/myOrder.html",
			controller: "order"
		})
		.state("main.after_sales", { //个人中心页面跳转到售后
			url: "/after_sales",
			templateUrl: "html/mine/after_sales.html",
			controller: "after_sales"
		})
		.state("main.wait_comment", { //个人中心页面跳转待评论
			url: "/wait_comment",
			templateUrl: "html/mine/wait_comment.html",
			controller: "wait_comment"
		})
		.state("main.wait_pay", { //个人中心足迹页面
			url: "/wait_pay",
			templateUrl: "html/mine/wait_pay.html",
			controller: "wait_pay"
		})
		.state("main.footprint", { //个人中心足迹页面
			url: "/footprint",
			templateUrl: "html/mine/footprint.html",
			controller: "footprint"
		})
		.state("main.collection", { //个人中心收藏页面
			url: "/collection",
			templateUrl: "html/mine/collection.html",
			controller: "collection"
		})
		.state("main.name", { //个人信息填写页面跳转
			url: "/name",
			templateUrl: "html/name.html",
			controller: "name"
		})
		.state("main.search", { //搜索页面跳转
			url: "/search",
			templateUrl: "html/search.html",
			controller: "search"
		})
		.state("main.searchlist", { //搜索页面跳转
			url: "/searchlist/:name",
			templateUrl: "html/searchlist.html",
			controller: "searchlist"
		})
		.state("main.routeInfo", { //商品详情页面跳转
			url: "/routeInfo/:Id",
			templateUrl: "html/routeInfo.html",
			controller: "routeInfo"
		})
		.state("main.route", { //路线页面跳转
			url: "/route",
			templateUrl: "html/route.html",
			controller: "route"
		})
		.state("main.project", { //项目列表跳转
			url: "/project",
			templateUrl: "html/project.html",
			controller: "project"
		})
		.state("main.shopcar", { //购物车页面路由跳转
			url: "/shopcar",
			templateUrl: "html/shopcar.html",
			controller: "shopcar"
		})
		.state("main.projectInfo", { //项目详情页面跳转
			url: "/projectInfo/:Id",
			templateUrl: "html/projectInfo.html",
			controller: "projectInfo"
		})
		.state("main.newprojectInfo", { //项目详情页面跳转
			url: "/newprojectInfo/:Id",
			templateUrl: "html/newprojectInfo.html",
			controller: "newprojectInfo"
		})
		.state("main.login", { //登录页面跳转
			url: "/login",
			templateUrl: "html/login/login.html",
			controller: "login"
		})
		.state("main.register", { //注册页面跳转
			url: "/register",
			templateUrl: "html/login/register.html",
			controller: "register"
		})
		.state("main.registration", { //第三方注册页面跳转
			url: "/registration",
			templateUrl: "html/login/registration.html",
			controller: "registration"
		})
		.state("main.orderInfor", { //订单详情页面页面跳转
			url: "/orderInfor/:Id",
			templateUrl: "html/mine/orderInfor.html",
			controller: "orderInfor"
		})
		.state("main.after_service", { //申请售后服务
			url: "/after_service/:id",
			templateUrl: "html/mine/after_service.html",
			controller: "after_service"
		})
		.state("main.headLineNews", { //申请售后服务
			url: "/headLineNews/:Id/:type",
			templateUrl: "html/headLineNews.html",
			controller: "headNews"
		})
		.state("main.payment", { //结算页面
			url: "/payment/:str/:num",
			templateUrl: "html/payment.html",
			controller: "payment"
		})
		.state("main.mywallet", { //钱包页面
			url: "/mywallet",
			templateUrl: "html/wallet/myWallet.html",
			controller: "mywallet"
		})
		.state("main.bankCard", { //提现到银行卡页面
			url: "/bankCard",
			templateUrl: "html/wallet/bankCard.html",
			controller: "bankCard"
		})
		.state("main.withdrawal", { //提现页面
			url: "/withdrawal",
			templateUrl: "html/wallet/withdrawal.html",
			controller: "withdrawal"
		})
		.state("main.apply", { //提现页面
			url: "/apply",
			templateUrl: "html/wallet/apply.html",
			controller: "apply"
		})
		.state("main.applytrue", { //提现页面
			url: "/applytrue/:money",
			templateUrl: "html/wallet/applytrue.html",
			controller: "applytrue"
		})
		.state("main.chooseBank", { //提现页面
			url: "/chooseBank/:type",
			templateUrl: "html/wallet/chooseBank.html",
			controller: "chooseBank"
		})
		.state("main.Bank", { //提现页面
			url: "/Bank",
			templateUrl: "html/wallet/Bank.html",
			controller: "Bank"
		})
		.state("main.unbundling", { //提现页面
			url: "/unbundling/:Id",
			templateUrl: "html/wallet/unbundling.html",
			controller: "unbundling"
		})
		.state("main.balance", { //提现页面
			url: "/balance",
			templateUrl: "html/wallet/balance.html",
			controller: "balance"
		})
		.state("main.weixinPay", { //微信支付跳转的新页面
			url: "/weixinPay/:openid",
			templateUrl: "html/weixinPay.html",
			controller: "weixinPay"
		})
		.state("main.tuxedo", { //个人中心参团或者查看开团页面
			url: "/tuxedo/:is_p",
			templateUrl: "html/mine/group/tuxedo.html",
			controller: "tuxedo"
		})
		.state("main.personal", { //个人中心参团或者查看开团页面
			url: "/personal",
			templateUrl: "html/mine/personalData.html",
			controller: "personal"
		})
		.state("main.setloginpsd", { //修改登陆密码
			url: "/setloginpsd",
			templateUrl: "html/login/setloginpsd.html",
			controller: "setpsd"
		})
		.state("main.phoneYZ", { //修改支付密码手机短信验证码
			url: "/phoneYZ/:type",
			templateUrl: "html/login/phoneYZ.html",
			controller: "phoneyz"
		})
		.state("main.forgetPassword", { //项目评论列表页
			url: "/forgetPassword",
			templateUrl: "html/login/forgetPassword.html",
			controller: "forgetPassword"
		})
		.state("main.setpaypsd", { //修改支付密码
			url: "/setpaypsd",
			templateUrl: "html/login/setpaypsd.html",
			controller: "setpaypsd"
		})
		.state("main.routcomment", { //线路评价
			url: "/routcomment/:Id/:id/:types/:type",
			templateUrl: "html/mine/Linereview/routcomment.html",
			controller: "routcomment"
		})
		.state("main.setphone", { //验证原有手机号
			url: "/setphone",
			templateUrl: "html/login/setphone.html",
			controller: "setphone"
		})
		.state("main.setNewphone", { //设置新手机号
			url: "/setNewphone",
			templateUrl: "html/login/setNewphone.html",
			controller: "setNewphone"
		})
		.state("main.Real-name", { //设置新手机号
			url: "/Real-name",
			templateUrl: "html/mine/Real-name.html",
			controller: "Real-name"
		})
		.state("main.groupInfo", { //我的团详情
			url: "/groupInfo/:Id",
			templateUrl: "html/mine/group/groupInfo.html",
			controller: "groupInfo"
		})
		.state("main.myNews", { //个人中心右上角我的消息
			url: "/myNews",
			templateUrl: "html/mine/myNews.html",
			controller: "myNews"
		})
		.state("main.myNewsinfo", { //我的消息点击去消息详情页
			url: "/myNewsinfo/:Id",
			templateUrl: "html/mine/myNewsinfo.html",
			controller: "myNewsinfo"
		})
		.state("main.serviceList", { //首页图标企业详情
			url: "/serviceList/:id/:name",
			templateUrl: "html/home/serviceList.html",
			controller: "serviceList"
		})
		.state("main.serListinfo", { //首页图标企业详情
			url: "/serListinfo/:id/:num",
			templateUrl: "html/home/serListinfo.html",
			controller: "serListinfo"
		})
		.state("main.Mortgage", { //房贷计算
			url: "/Mortgage",
			templateUrl: "html/wallet/Mortgage.html",
			controller: "Mortgage"
		})
		.state("main.qrcode", { //我的二维码
			url: "/qrcode",
			templateUrl: "html/mine/qrcode.html",
			controller: "qrcode"
		})
		.state("main.paySuccess", { //支付成功
			url: "/paySuccess",
			templateUrl: "html/paySuccess.html",
			controller: "paySuccess"
		})
		.state("main.payError", { //支付失败
			url: "/payError",
			templateUrl: "html/payError.html",
			controller: "payError"
		})
		.state("main.new_info", { //轮播图外链
			url: "/new_info/:id",
			templateUrl: "html/new_info.html",
			controller: "new_info"
		})
		.state("main.routecommentlist", { //线路评论列表页
			url: "/routecommentlist/:Id",
			templateUrl: "html/mine/Linereview/routecommentlist.html",
			controller: "routecommentlist"
		})
		.state("main.projectcomment", { //项目评论页
			url: "/projectcomment/:id",
			templateUrl: "html/mine/Linereview/projectcomment.html",
			controller: "projectcomment"
		})
		.state("main.projectcommentlist", { //项目评论列表页
			url: "/projectcommentlist/:id",
			templateUrl: "html/mine/Linereview/projectcommentlist.html",
			controller: "projectcommentlist"
		})
		.state("main.clause", { //项目评论列表页
			url: "/clause",
			templateUrl: "html/home/clause.html",
			controller: "clause"
		})
}])
app.config(['$httpProvider', function($httpProvider) {
	$httpProvider.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded;charset=utf-8";
	$httpProvider.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
	var param = function(obj) {
		var query = "",
			name, value, fullSubName, subName, subValue, innerObj, i;
		for(name in obj) {
			value = obj[name];
			if(value instanceof Array) {
				for(i = 0; i < value.length; ++i) {
					subValue = value[i];
					fullSubName = name + "[" + i + "]";
					innerObj = {};
					innerObj[fullSubName] = subValue;
					query += param(innerObj) + "&";
				}
			} else if(value instanceof Object) {
				for(subName in value) {
					subValue = value[subName];
					fullSubName = name + "[" + subName + "]";
					innerObj = {};
					innerObj[fullSubName] = subValue;
					query += param(innerObj) + "&";
				}
			} else if(value !== undefined && value !== null) {
				query += encodeURIComponent(name) + "=" + encodeURIComponent(value) + "&";
			}
		}
		return query.length ? query.substr(0, query.length - 1) : query;
	};
	$httpProvider.defaults.transformRequest = [function(data) {
		return angular.isObject(data) && String(data) !== "[object File]" ? param(data) : data;
	}];
}])
app.filter('trustHtml', function($sce) {　　
	return function(input) {　　　　
		return $sce.trustAsHtml(input);　　
	}
});
app.controller("main", ["$scope", "$rootScope", "$state", "$http", "$location", "$timeout", function($scope, $rootScope, $state, $http, $location, $timeout) {
	scrollTo(0, 0);
	var browser = {
		versions: function() {
			var u = navigator.userAgent,
				app = navigator.appVersion;
			return { //移动终端浏览器版本信息
				trident: u.indexOf('Trident') > -1, //IE内核
				presto: u.indexOf('Presto') > -1, //opera内核
				webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
				gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
				mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端
				ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
				android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或uc浏览器
				iPhone: u.indexOf('iPhone') > -1, //是否为iPhone或者QQHD浏览器
				iPad: u.indexOf('iPad') > -1, //是否iPad
				webApp: u.indexOf('Safari') == -1 //是否web应该程序，没有头部与底部
			};
		}(),
		language: (navigator.browserLanguage || navigator.language).toLowerCase()
	}
	if(browser.versions.mobile) { //判断是否是移动设备打开。browser代码在下面
		var ua = navigator.userAgent.toLowerCase(); //获取判断用的对象
		if(ua.match(/MicroMessenger/i) == "micromessenger") {
			//在微信中打开
			localStorage.setItem("channel", 30003)
		} else {
			localStorage.setItem("channel", 30002)
		}
	}
	$rootScope.star = function(n) {
		var con_wid = document.getElementById("star_con").offsetWidth;
		var del_star = document.getElementById("del_star");
		//透明星星移动的像素
		var del_move = (n * con_wid) / 10;

		del_star.style.backgroundPosition = -del_move + "px 0px";
		del_star.style.left = del_move + "px";
	}

	$rootScope.page = {};
	//配置通用地址跳转
	$rootScope.page.go = function(path) {
			$location.path("/" + path);
		}
		//通用返回上一页
	$rootScope.page.toHistory = function() {
			history.back()
		}
		//默认通用地址
	$rootScope.Location = "http://web.tl178.cn/"
		//获取用户信息
	$rootScope.userID = localStorage.getItem("user_ID");
	$rootScope.token = localStorage.getItem("token")
		//封装判断登录
	$rootScope.register = function() {
		localStorage.setItem("url", window.location.href)
		$(".wrapLoginDialog").show();
		$(".cancle").click(function() {
			$(".wrapLoginDialog").hide();
		})
		$(".sure").click(function() {
			$(".wrapLoginDialog").hide();
			$state.go("main.login");
		})
	}

	//封装判断重复登录
	$rootScope.login = function(data) {
			if(data["status"] == 0) {
				$(".wrapLoginDialog").show();
				$(".cancle").click(function() {
					$(".wrapLoginDialog").hide();
				})
				$(".sure").click(function() {
					$(".wrapLoginDialog").hide();
					$state.go("main.login");
				})
			}
			if(data["status"] == 4) {
				$(".wrapLoginDialog2").show();
				$(".cancle").click(function() {
					$(".wrapLoginDialog2").hide();
				})
				$(".sure").click(function() {
					$(".wrapLoginDialog2").hide();
					$state.go("main.login");
				})
				$rootScope.message = data["message"]
			}
			if(data["status"] == 5) {
				$(".wrapLoginDialog2").show();
				$(".cancle").click(function() {
					$(".wrapLoginDialog2").hide();
				})
				$(".sure").click(function() {
					$(".wrapLoginDialog2").hide();
					$state.go("main.login");
				})
				$rootScope.message = data["message"]
			}
		}
		//查询购物车数量
	$rootScope.findshopcarNum = function() {
		$http({
			method: 'post',
			url: '' + $rootScope.Location + 'Order/cart_list',
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			data: {
				user_Id: localStorage.getItem("user_ID"),
				token: localStorage.getItem("token")
			}
		}).success(function(data) {
			$rootScope.shopcarNum = data["num"]
			if(data["num"] >= 1) {
				$rootScope.page.shopcarNum = true
			} else {
				$rootScope.page.shopcarNum = false
			}
			if(data["status"]==4){
				$(".wrapLoginDialog2").show();
				$(".cancle").click(function() {
				$(".wrapLoginDialog2").hide();
				})
				$(".sure").click(function() {
				$(".wrapLoginDialog2").hide();
					$state.go("main.login");
				})
				$rootScope.message = data["message"]
			}
		})
	}
	$rootScope.findshopcarNum()

	$scope.tomine = function() {
		$state.go("main.mine")
	}
	$scope.tohome = function() {
		$state.go("main.home")
	}
	$scope.toname = function() {
		$state.go("main.name")
	}
	$scope.toroute = function() {
		$state.go("main.route")
	}
	$scope.toproject = function() {
		$state.go("main.project")
	}
	$scope.tosearch = function() {
		$state.go("main.search")
	}
	$scope.toshopcar = function() {
			$state.go("main.shopcar")
	}
	$rootScope.record = function(){
		if($location.search().fff){
			var fff = $location.search().fff
			$http({
					method: 'post',
					url: '' + $rootScope.Location + 'Share/addr_clicks',
					headers: {
						'Content-Type': 'application/x-www-form-urlencoded'
					},
					data: {
						other_Id: fff,
						addr:$location.absUrl()
					}
				}).success(function(data){

				})
		}		
	}
	$rootScope.shareSuccess = function(type){
		if(localStorage.getItem("user_ID")){	
				if($location.search().id){
					var absurl = "http://app.tl178.cn/#/home/?fff=" + localStorage.getItem("user_ID") + "";	
				}else{
					var absurl =$location.absUrl()+"?fff=" + localStorage.getItem("user_ID") + ""
				}		
			}else{
				var absurl =$location.absUrl()
			}
		$http({
			method: 'post',
			url: '' + $rootScope.Location + 'Share/share_record',
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			data: {
				user_Id:localStorage.getItem("user_ID"),
				addr:absurl,
				share_type:type
			}
		}).success(function(data){
			if(data["status"]==1){
				
			}	
		})
	}
		//微信分享
	$rootScope.share = function() {
		$http({
			method: 'post',
			url: '' + $rootScope.Location + 'Share/share_s',
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			data: {
				url:$location.absUrl()
			}
		}).success(function(result) {
			var appid = result.appId;
			var timestamp = result.timestamp;
			var nonceStr = result.nonceStr;
			var signature = result.signature;
			if(localStorage.getItem("user_ID")){	
				if($location.search().id){
					var urlAfter = "http://app.tl178.cn/#/home/?fff=" + localStorage.getItem("user_ID") + "";	
				}else{
					var urlAfter =$location.absUrl()+"?fff=" + localStorage.getItem("user_ID") + ""
				}		
			}else{
				var urlAfter =$location.absUrl()
			}
			console.log(urlAfter)
			var title = "天乐网";
			var imgUrl = "http://web.tl178.cn/Public/admin/share/sharelogo.jpg";
			var desc = "大自然是您最好的家!";
			wx.config({
				debug: false, //开启调试模式,调用的所有api的返回值会在客户端alert出来
				appId: appid, // 必填，公众号的唯一标识
				timestamp: timestamp, // 必填，生成签名的时间戳
				nonceStr: nonceStr, // 必填，生成签名的随机串
				signature: signature, // 必填，签名
				jsApiList: [
					'onMenuShareTimeline', //分享到朋友圈
					'onMenuShareAppMessage', //分享给朋友
					'onMenuShareQQ', //分享给QQ好友
					'showOptionMenu'
				]
			});

			wx.ready(function() {
				wx.showOptionMenu();
				wx.onMenuShareTimeline({
					title: title,
					desc: desc, // 分享标题							
					link: urlAfter, // 分享链接
					imgUrl: imgUrl, // 分享图片
					success: function() {
						$rootScope.shareSuccess(2)
						// 用户确认分享后执行的回调函数
					},
					cancel: function() {
						// 用户取消分享后执行的回调函数
					}
				});

				wx.onMenuShareAppMessage({
					title: title, // 分享标题
					desc: desc, // 分享描述
					link: urlAfter, // 分享链接
					imgUrl: imgUrl, // 分享图片
					type: '', // 分享类型,music、video或link，不填默认为link
					dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
					success: function() {
						$rootScope.shareSuccess(1)
						// 用户确认分享后执行的回调函数
					},
					cancel: function() {
						// 用户取消分享后执行的回调函数
					}
				});

				wx.onMenuShareQQ({
					title: title, // 分享标题
					desc: desc, // 分享描述
					link: urlAfter, // 分享链接
					imgUrl: imgUrl, // 分享图标
					success: function(res) {
						$rootScope.shareSuccess(3)
						// 用户确认分享后执行的回调函数
					},
					cancel: function(res) {
						// 用户取消分享后执行的回调函数
					}
				});
			});
			wx.error(function(res) {
				console.log(res)
			});
		})
	}
	$rootScope.share()
}])